package com.salon.modules.app.controller;

import com.salon.common.http.response.ObjectResult;
import com.salon.common.validator.ValidatorUtils;
import com.salon.modules.app.form.RegisterForm;
import com.salon.modules.makeup.entity.UserEntity;
import com.salon.modules.makeup.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 注册
 */
@RestController
@RequestMapping("/app")
@Api(tags = "注册接口")
public class RegisterController {

    @Resource
    private UserService userService;

    @PostMapping("register")
    @ApiOperation("用户注册")
    public ObjectResult<String> register(@RequestBody RegisterForm form) {
        ValidatorUtils.validateEntity(form);
        UserEntity user = new UserEntity();
        user.setMobile(form.getMobile());
        user.setUsername(form.getMobile());
        user.setPassword(DigestUtils.sha256Hex(form.getPassword()));
        userService.save(user);
        return ObjectResult.success();
    }

}
